package com.my.javaweb.interview.date240410.answer2;


// 订单业务类型枚举 type-业务类型，factory-业务对应策略工厂
enum OrderTypeEnum {
    HOTEL("hotel", HotelOrderStrategy::new),
    TICKETS("tickets", TicketsOrderStrategy::new),
    VISA("visa", VisaOrderStrategy::new);

    private String type;
    private OrderStrategyFactory factory;

    OrderTypeEnum(String type, OrderStrategyFactory factory) {
        this.type = type;
        this.factory = factory;
    }

    // 更改为获取枚举类型本身，而不是工厂
    public static OrderTypeEnum getStrategyByType(String orderBizType) {
        for (OrderTypeEnum value : values()) {
            if (value.type.equals(orderBizType)) {
                return value;
            }
        }
        throw new IllegalArgumentException("Invalid order biz type: " + orderBizType);
    }

    public OrderStrategyFactory getStrategyFactory() {
        return factory;
    }
}

// 定义策略接口
interface OrderStrategy {
    void handlePayment(OrderInfo orderInfo);
    void handleCompletion(OrderInfo orderInfo);
}

// 策略的具体实现类
class HotelOrderStrategy implements OrderStrategy {
    // 实现支付和完成逻辑...
    @Override
    public void handlePayment(OrderInfo orderInfo) {

    }
    @Override
    public void handleCompletion(OrderInfo orderInfo) {

    }
}

class TicketsOrderStrategy implements OrderStrategy {
    // 实现支付和完成逻辑...
    @Override
    public void handlePayment(OrderInfo orderInfo) {

    }
    @Override
    public void handleCompletion(OrderInfo orderInfo) {

    }
}

class VisaOrderStrategy implements OrderStrategy {
    // 实现支付和完成逻辑...
    @Override
    public void handlePayment(OrderInfo orderInfo) {

    }
    @Override
    public void handleCompletion(OrderInfo orderInfo) {

    }
}

// 策略工厂接口
interface OrderStrategyFactory {
    OrderStrategy create();
}

// 订单状态MQ监听器重构版
public class OrderStatusMQListener {
    private final OrderService orderService;
    private final PayService payService;
    private final NotifyService notifyService;

    public OrderStatusMQListener(OrderService orderService, PayService payService, NotifyService notifyService) {
        this.orderService = orderService;
        this.payService = payService;
        this.notifyService = notifyService;
    }

    public void consume(String orderNo) {
        OrderInfo orderInfo = orderService.getOrderInfo(orderNo);
        //通过业务类型获取业务枚举
        OrderTypeEnum typeEnum = OrderTypeEnum.getStrategyByType(orderInfo.getOrderBizType());
        //通过枚举获取对应策略
        OrderStrategy strategy = typeEnum.getStrategyFactory().create();

        switch (orderInfo.getStatus()) {
            case 1: // 创建，待支付执行支付
                strategy.handlePayment(orderInfo);
                break;
            case 4: // 订单完成
                strategy.handleCompletion(orderInfo);
                break;
            case 9: // 待未来业务扩展...
                // 处理同意退款等逻辑
                break;
            default:
                throw new IllegalStateException("Unexpected order status: " + orderInfo.getStatus());
        }
    }
}



// -------------------- 方便候选人本地阅读代码，↓↓↓↓↓↓↓不需要动 --------------------

/**
 * 订单服务类 (方便候选人本地阅读代码, 不需要动)
 */
class OrderService {
    OrderInfo getOrderInfo(String orderNo) {
        return new OrderInfo();
    }

    void updateOrderStatus(String orderNo, int status) {
        // 更新订单逻辑
    }

}

/**
 * 支付服务类 (方便候选人本地阅读代码, 不需要动)
 */
class PayService {

    /**
     * 信用支付
     * @param orderInfo
     */
    long payOfCredit(OrderInfo orderInfo) {
        //  信用支付逻辑，候选人不用理会支付内部逻辑 ...
        return 1;
    }

    /**
     * 支付宝支付
     * @param orderInfo
     */
    long payOfAliPay(OrderInfo orderInfo) {
        //  支付宝支付逻辑，候选人不用理会支付内部逻辑 ...
        return 1;
    }

    /**
     * 未支付成功后关闭
     * @param orderInfo
     */
    public void notPayedToClose(OrderInfo orderInfo) {
        //  支付宝支付逻辑，候选人不用理会支付内部逻辑 ...
    }
}

class NotifyService {
    /**
     * 短信通知
     */
    void sms(String msg) {}
    /**
     * 钉钉通知
     */
    void dingding(String msg) {}
    /**
     * 微信通知
     */
    void wechat(String msg) {}
    /**
     * 邮件通知
     */
    void email(String msg) {}
}


/**
 * 订单类 (方便候选人本地阅读代码, 不需要动)
 */
class OrderInfo {
    private String orderNo;
    private String orderBizType;
    private int status;
    private String anything;

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public String getOrderBizType() {
        return orderBizType;
    }

    public void setOrderBizType(String orderBizType) {
        this.orderBizType = orderBizType;
    }

    public String getAnything() {
        return anything;
    }

    public void setAnything(String anything) {
        this.anything = anything;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}